package com.crealth.db;

import java.util.List;

import org.apache.log4j.Logger;

import com.crealth.manager.MybatisManager;
import com.crealth.model.ClienteleInfoModel;


public class MyselfClienteleDb {
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static Integer insertClienteleInfo(String strMobi, String strNoti, 
			String strName, Integer createrID) {
		Integer res = null;
		try {
			res = MybatisManager.myselfClienteleInfoMapper.insertClienteleInfo(strMobi, strNoti, strName, createrID);
			MybatisManager.commit();
		} catch (Exception e) {
			errorLog.error("", e);
		}
		
		return res;
	}
    
    public static Integer insertClienteleObj(ClienteleInfoModel cliModel) {
    	Integer res = null;
		try {
			res = MybatisManager.myselfClienteleInfoMapper.insertClienteleObj(cliModel);
			MybatisManager.commit();
		} catch (Exception e) {
			errorLog.error("", e);
		}
		
		return res;
    }
    
    public static int addClienteleModel(int userID, List<ClienteleInfoModel> cliModels) {		
		int insertNum = 0;
		try {
			for (int idx = 0; idx < cliModels.size(); ++idx) {
				ClienteleInfoModel model = cliModels.get(idx);
				model.setCreaterUserID(userID);
				Integer id = insertClienteleObj(model);
				if (id != null) {
					insertNum++;
				}
			}
			MybatisManager.commit();
		}
		catch (Exception e) {
			errorLog.error("", e);
		}
		
		return insertNum;
	}
    
    public static List<ClienteleInfoModel> selectClienteleInfoByCreateUser(Integer userID) {
    	List<ClienteleInfoModel> infols = MybatisManager.myselfClienteleInfoMapper.selectClienteleInfoByCreateUser(userID);
    	
    	return infols;
    }
    
    public static List<ClienteleInfoModel> selectClienteleInfoByMobi(String strMobi, int userID) {
    	List<ClienteleInfoModel> infols = MybatisManager.myselfClienteleInfoMapper.selectClienteleInfoByMobi(strMobi, userID);
    	
    	return infols;
    }
    
    public static Integer updateCallRecordByMobi(int setUserID, String callMobi, long cdrID) {
    	Integer res = MybatisManager.myselfClienteleInfoMapper.updateCallRecordByMobi(setUserID, callMobi, cdrID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer selectUserClienteleSumNum(int userID) {
    	Integer res = MybatisManager.myselfClienteleInfoMapper.selectUserClienteleSumNum(userID);
    	
    	return res;
    }
    
    public static Integer selectUserClienteleNotCallNum(int userID) {
    	Integer res = MybatisManager.myselfClienteleInfoMapper.selectUserClienteleNotCallNum(userID);
    	
    	return res;
    }
    
    public static void deleteClienteleByCreateID(int userID) {
    	MybatisManager.myselfClienteleInfoMapper.deleteClienteleByCreateID(userID);
    	MybatisManager.commit();
    }
    
    
}
